package com.fastwire.carriere.common.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fastwire.carriere.inventory.entity.Equipment;
import com.fastwire.carriere.inventory.entity.NetworkElement;
import com.fastwire.carriere.inventory.entity.TerminationPoint;

// TODO: Auto-generated Javadoc
/**
 * The Class CSVParser.
 */
public class CSVParser {

	/**
	 * Parses the ne file.
	 *
	 * @param fileName the file name
	 * @return the list
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public static List<NetworkElement> parseNEFile(String fileName)
			throws IOException {
		List<NetworkElement> neList = new ArrayList<NetworkElement>();
		List<String[]> tokenValuesList = parseCXVFile(fileName);

		for (String[] tokenValues : tokenValuesList) {

			if (tokenValues.length > 3) {
				NetworkElement networkElement = new NetworkElement();
				networkElement.setNeType(tokenValues[0]);
				if (tokenValues.length > 8) {
					networkElement.setIpAddress(tokenValues[1]);
					networkElement.setName(tokenValues[6]);
					networkElement.setEmsNativeName(tokenValues[7]);
				} else {
					networkElement.setName(tokenValues[5]);
					networkElement.setEmsNativeName(tokenValues[6]);
				}

				neList.add(networkElement);
			}

		}

		return neList;
	}

	/**
	 * Parses the equipment file.
	 *
	 * @param fileName the file name
	 * @param neNativeName the ne native name
	 * @return the list
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public static List<Equipment> parseEquipmentFile(String fileName,
			String neNativeName) throws IOException {

		Pattern pattern = Pattern.compile(".*slot=(\\d+).*");

		HashMap<String, Equipment> eqMap = new HashMap<String, Equipment>();

		List<String[]> tokenValuesList = parseCXVFile(fileName);

		// read comma separated file line by line
		for (String[] tokenValues : tokenValuesList) {
			if (tokenValues.length > 1) {
				Equipment equipment = new Equipment();

				equipment.setName(tokenValues[0]);
				equipment.setEmsNativeName(tokenValues[1]);
				if (tokenValues[0].contains("EQ=1")) {
					equipment.setEquipmentType(tokenValues[1]);

				}
				Matcher matcher = pattern.matcher(tokenValues[0]);
				if (matcher.matches()) {
					equipment.setSlotId(matcher.group(1));
				}

				eqMap.put(equipment.getSlotId(), equipment);

			}

		}

		return new ArrayList<Equipment>(eqMap.values());
	}

	/**
	 * Parses the termination point file.
	 *
	 * @param fileName the file name
	 * @param neNativeName the ne native name
	 * @return the list
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public static List<TerminationPoint> parseTerminationPointFile(
			String fileName, String neNativeName) throws IOException {

		/*
		 * 
		 * List<TerminationPoint> tpList = new ArrayList<TerminationPoint>();
		 * List<String[]> tokenValuesList = parseCXVFile(fileName);
		 * 
		 * // read comma separated file line by line for(String[] tokenValues:
		 * tokenValuesList){
		 * 
		 * if (tokenValues.length > 1) { TerminationPoint terminationPoint = new
		 * TerminationPoint(); terminationPoint.setLayerRate(tokenValues[0]);
		 * terminationPoint.setName(tokenValues[1]); if (tokenValues.length > 2)
		 * { terminationPoint.setEmsNativeName(tokenValues[2]); }
		 * 
		 * tpList.add(terminationPoint);
		 * 
		 * }
		 * 
		 * }
		 * 
		 * return tpList;
		 */
		return null;
	}

	/**
	 * Parses the cxv file.
	 *
	 * @param fileName the file name
	 * @return the list
	 * @throws FileNotFoundException the file not found exception
	 */
	private static List<String[]> parseCXVFile(String fileName)
			throws FileNotFoundException {
		BufferedReader br = new BufferedReader(new FileReader(fileName));
		String strLine = "";
		StringTokenizer st = null;
		int lineNumber = 0, tokenNumber = 0;

		// Pattern pattern = Pattern.compile(".*slot=(\\d+).*");

		List<String[]> tokenValuesList = new ArrayList<String[]>();

		// read comma separated file line by line
		try {
			while ((strLine = br.readLine()) != null) {
				lineNumber++;

				// break comma separated line using ","
				st = new StringTokenizer(strLine, ",");

				String[] tokenValues = new String[st.countTokens()];

				while (st.hasMoreTokens()) {

					tokenValues[tokenNumber++] = st.nextToken().trim();
				}

				// reset token number
				tokenNumber = 0;

				tokenValuesList.add(tokenValues);

			}
		} catch (IOException e) {
			CommonLogger.error(CSVParser.class, e.getMessage());
			e.printStackTrace();
		}

		return tokenValuesList;
	}

}
